<template>
  <div class="game-ui">
    <div class="health-bars">
      <div class="health-bar player1">
        <div class="health-fill" :style="{ width: player1Health + '%' }"></div>
        <span class="health-text">{{ player1Health }}%</span>
      </div>
      <div class="timer">
        <span>{{ formattedTime }}</span>
      </div>
      <div class="health-bar player2">
        <div class="health-fill" :style="{ width: player2Health + '%' }"></div>
        <span class="health-text">{{ player2Health }}%</span>
      </div>
    </div>
    
    <div class="round-display">
      <span>ROUND {{ round }}</span>
    </div>
    
    <button class="menu-button" @click="$emit('back-to-menu')">MENU</button>
    
    <div v-if="gameOver" class="game-over">
      <h2>{{ winner }} WINS!</h2>
      <button @click="restartRound">NEXT ROUND</button>
    </div>
  </div>
</template>

<script>
import { computed, watch } from 'vue'
import {ref} from 'vue'
export default {
  props: {
    player1Health: Number,
    player2Health: Number,
    round: Number,
    time: Number
  },
  emits: ['back-to-menu', 'restart-round'],
  setup(props, { emit }) {
    const gameOver = ref(false)
    const winner = ref('')
    
    const formattedTime = computed(() => {
      return Math.max(0, Math.floor(props.time)).toString().padStart(2, '0')
    })
    
    watch(() => props.player1Health, (health) => {
      if (health <= 0) {
        gameOver.value = true
        winner.value = 'PLAYER 2'
      }
    })
    
    watch(() => props.player2Health, (health) => {
      if (health <= 0) {
        gameOver.value = true
        winner.value = 'PLAYER 1'
      }
    })
    
    watch(() => props.time, (time) => {
      if (time <= 0) {
        gameOver.value = true
        winner.value = props.player1Health > props.player2Health ? 'PLAYER 1' : 
                      props.player2Health > props.player1Health ? 'PLAYER 2' : 'DRAW'
      }
    })
    
    const restartRound = () => {
      gameOver.value = false
      emit('restart-round')
    }
    
    return {
      formattedTime,
      gameOver,
      winner,
      restartRound
    }
  }
}
</script>

<style lang="scss" scoped>
.game-ui {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  
  .health-bars {
    display: flex;
    justify-content: space-between;
    padding: 20px;
    
    .health-bar {
      width: 40%;
      height: 30px;
      background: rgba(0, 0, 0, 0.5);
      border: 2px solid #444;
      position: relative;
      
      .health-fill {
        height: 100%;
        transition: width 0.3s ease;
      }
      
      .health-text {
        position: absolute;
        right: 10px;
        top: 50%;
        transform: translateY(-50%);
        color: white;
        font-size: 14px;
        text-shadow: 1px 1px 2px black;
      }
    }
    
    .player1 .health-fill {
      background: linear-gradient(to right, #ff3333, #ff9999);
    }
    
    .player2 .health-fill {
      background: linear-gradient(to right, #3399ff, #33ccff);
    }
    
    .timer {
      width: 15%;
      display: flex;
      justify-content: center;
      align-items: center;
      background: rgba(0, 0, 0, 0.7);
      border: 2px solid #ffcc00;
      color: #ffcc00;
      font-size: 20px;
    }
  }
  
  .round-display {
    position: absolute;
    top: 70px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.7);
    padding: 5px 20px;
    border: 2px solid #ff5555;
    color: #ff5555;
    font-size: 18px;
    border-radius: 20px;
  }
  
  .menu-button {
    position: absolute;
    top: 20px;
    right: 20px;
    padding: 8px 16px;
    background: rgba(0, 0, 0, 0.7);
    border: 2px solid #fff;
    color: #fff;
    font-family: 'Press Start 2P', cursive;
    font-size: 12px;
    cursor: pointer;
    pointer-events: auto;
    transition: all 0.3s ease;
    
    &:hover {
      background: rgba(255, 255, 255, 0.2);
    }
  }
  
  .game-over {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(0, 0, 0, 0.8);
    padding: 30px;
    border: 4px solid #ffcc00;
    text-align: center;
    pointer-events: auto;
    
    h2 {
      color: #ffcc00;
      margin-bottom: 20px;
      font-size: 32px;
      text-shadow: 0 0 10px rgba(255, 204, 0, 0.5);
    }
    
    button {
      padding: 10px 20px;
      background: linear-gradient(to bottom, #ff5555, #cc0000);
      border: none;
      color: white;
      font-family: 'Press Start 2P', cursive;
      font-size: 14px;
      cursor: pointer;
      transition: all 0.3s ease;
      
      &:hover {
        transform: scale(1.05);
        box-shadow: 0 0 15px rgba(255, 85, 85, 0.7);
      }
    }
  }
}
</style>